﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ViewProxy.cs" company="YellowFish">
//   Copyright © YellowFish 2009
// </copyright>
// <summary>
//   Defines the ViewProxy type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace MobilePal.Views
{
  #region Imported Namespaces


  using System;

  using State;


  #endregion


  /// <summary>
  /// Describes and manipulates the view proxy.
  /// </summary>
  public class ViewProxy
  {
    #region Constants and Fields


    private readonly ActivityCollection _activityCollection;
    private readonly ActivityCategoryCollection _categoryCollection;
    private readonly ApplicationView _applicationView;
    private readonly Func<ApplicationView, IView> _createView;

    private IView _view;


    #endregion


    #region Constructors and Destructors


    /// <summary>
    /// Initializes a new instance of the <see cref="ViewProxy"/> class.
    /// </summary>
    /// <param name="applicationView">The application view.</param>
    /// <param name="activityCollection">The activity collection.</param>
    /// <param name="categoryCollection">The category collection.</param>
    /// <param name="createView">The create view.</param>
    /// <exception cref="ArgumentNullException"><c>applicationView</c> is <c>null</c>.</exception>
    /// <exception cref="ArgumentNullException"><c>activityCollection</c> is <c>null</c>.</exception>
    /// <exception cref="ArgumentNullException"><c>createView</c> is <c>null</c>.</exception>
    public ViewProxy(ApplicationView applicationView, ActivityCollection activityCollection, ActivityCategoryCollection categoryCollection, 
                     Func<ApplicationView, IView> createView)
    {
      if (applicationView == null)
      {
        throw new ArgumentNullException("applicationView");
      }

      if (activityCollection == null)
      {
        throw new ArgumentNullException("activityCollection");
      }

      if (createView == null)
      {
        throw new ArgumentNullException("createView");
      }

      _applicationView = applicationView;
      _activityCollection = activityCollection;
      _categoryCollection = categoryCollection;
      _createView = createView;
    }


    #endregion


    #region Properties


    /// <summary>
    /// Gets the application view.
    /// </summary>
    /// <value>The application view.</value>
    public ApplicationView ApplicationView
    {
      get
      {
        if (_view != null)
        {
          _applicationView.Settings = _view.GetSettings();
        }

        return _applicationView;
      }
    }

    /// <summary>
    /// Gets a value indicating whether this instance is created.
    /// </summary>
    /// <value>
    /// Returns <c>true</c> if this instance is created; otherwise, <c>false</c>.
    /// </value>
    public bool IsCreated
    {
      get
      {
        return _view != null;
      }
    }


    /// <summary>
    /// Gets the view.
    /// </summary>
    /// <value>The view.</value>
    public IView View
    {
      get
      {
        if (_view == null)
        {
          _view = _createView(_applicationView);
          _view.Initialize(_activityCollection, _categoryCollection, _applicationView.Settings);

          // <RNovitsky, 13.08.2009>: Need this because the View might change the instance of settings
          // in case they were null, or of a wrong type.
          _applicationView.Settings = _view.GetSettings();
        }

        return _view;
      }
    }


    #endregion
  }
}